<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="designation">
        <el-input
          v-model="queryParams.designation"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['info:networkDeviceMgmt:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:networkDeviceMgmt:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:networkDeviceMgmt:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['info:networkDeviceMgmt:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="networkDeviceMgmtList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="neId" />
      <el-table-column label="名称" align="center" prop="designation" />
      <el-table-column label="型号" align="center" prop="typeSpecification" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="管理IP" align="center" prop="ip" />
      <el-table-column label="专案" align="center" prop="projectName" />
      <el-table-column label="位置" align="center" prop="location" />
      <!-- <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="更新人" align="center" prop="updateName" />
      <el-table-column label="预留" align="center" prop="reserved" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:networkDeviceMgmt:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:networkDeviceMgmt:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网络设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-row :gutter="15">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-row :gutter="1"> -->
          <el-col :span="10">
        <el-form-item label="名称" prop="designation">
          <el-input v-model="form.designation" placeholder="请输入名称" :style="{width: '97%'}" />
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="1"> -->
          <el-col :span="12">
        <el-form-item label="型号" prop="typeSpecification">
          <el-select v-model="form.typeSpecification" placeholder="请选择型号"
                          >
                <el-option v-for="(item, index) in typeSpecificationOptions" :key="index" :label="item"
                          :value="item" ></el-option>
              </el-select>
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="1"> -->
          <el-col :span="10">
        <el-form-item label="品牌" prop="brand">
          <el-select v-model="form.brand" placeholder="请选择品牌"
                          >
                <el-option v-for="(item, index) in brandOptions" :key="index" :label="item"
                          :value="item" ></el-option>
              </el-select>
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="1"> -->
          <el-col :span="12">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型"
                          >
                <el-option v-for="(item, index) in typeOptions" :key="index" :label="item"
                          :value="item" ></el-option>
              </el-select>
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="1"> -->
          <el-col :span="10">
        <el-form-item label="管理IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入管理IP" :style="{width: '97%'}" />
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="1"> -->
          <el-col :span="12">
        <el-form-item label="专案" prop="projectName">
          <el-select v-model="form.projectName" placeholder="请选择专案"
                          >
                <el-option v-for="(item, index) in projectNameOptions" :key="index" :label="item"
                          :value="item" ></el-option>
              </el-select>
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="1"> -->
          <el-col :span="10">
        <el-form-item label="位置" prop="location">
          <el-select v-model="form.location" placeholder="请选择位置"
                          >
                <el-option v-for="(item, index) in locationOptions" :key="index" :label="item"
                          :value="item" ></el-option>
              </el-select>
        </el-form-item>
        </el-col>
        <!-- </el-row> -->
        <!-- <el-row :gutter="9"> -->
          <el-col :span="20">
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description"  type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
        </el-col>
       
      </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { networkDeviceMgmtOpitons,listNetworkDeviceMgmt, getNetworkDeviceMgmt, delNetworkDeviceMgmt, addNetworkDeviceMgmt, updateNetworkDeviceMgmt, exportNetworkDeviceMgmt } from "@/api/info/networkDeviceMgmt";

export default {
  name: "NetworkDeviceMgmt",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 网络设备管理表格数据
      networkDeviceMgmtList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        designation: null,
        typeSpecification: null,
        description: null,
        brand: null,
        type: null,
        ip: null,
        projectName: null,
        location: null,
        createName: null,
        updateName: null,
        reserved: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        designation: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        typeSpecification: [
          { required: true, message: "型号不能为空", trigger: "blur" }
        ],
        brand: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        ip: [
          { required: true, message: "管理IP不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "专案不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "位置不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
      },
      typeSpecificationOptions: [],
      brandOptions: [],
      typeOptions: [],
      projectNameOptions: [],
      locationOptions: [],
    };
  },
  created() {
    this.getList();
    this.getOpitons();
  },
  methods: {
    /** 查询网络设备管理列表 */
    getList() {
      this.loading = true;
      listNetworkDeviceMgmt(this.queryParams).then(response => {
        this.networkDeviceMgmtList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        neId: null,
        designation: null,
        typeSpecification: null,
        description: null,
        brand: null,
        type: null,
        ip: null,
        projectName: null,
        location: null,
        createName: null,
        createTime: null,
        updateName: null,
        updateTime: null,
        reserved: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.neId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //获取下拉框数据
    getOpitons(){
      networkDeviceMgmtOpitons().then(response => {
        console.log(response)
        for(var x = 0;x<response.length;x ++){
          this.typeSpecificationOptions.push(response[x].typeSpecification);
          this.brandOptions.push(response[x].brand);
          this.typeOptions.push(response[x].type);
          this.projectNameOptions.push(response[x].projectName);
          this.locationOptions.push(response[x].location);
        }

        for(var i = 0; i < this.typeSpecificationOptions.length;i ++) {  
            for (let j = i + 1; j < this.typeSpecificationOptions.length;j ++) {
                if (this.typeSpecificationOptions[i] == this.typeSpecificationOptions[j]) {
                    this.typeSpecificationOptions.splice(j, 1);
                    j--;
                }
            }
        }
        for(var i = 0; i < this.brandOptions.length;i ++) {  
            for (let j = i + 1; j < this.brandOptions.length;j ++) {
                if (this.brandOptions[i] == this.brandOptions[j]) {
                    this.brandOptions.splice(j, 1);
                    j--;
                }
            }
        }
        for(var i = 0; i < this.typeOptions.length;i ++) {  
            for (let j = i + 1; j < this.typeOptions.length;j ++) {
                if (this.typeOptions[i] == this.typeOptions[j]) {
                    this.typeOptions.splice(j, 1);
                    j--;
                }
            }
        }
        for(var i = 0; i < this.projectNameOptions.length;i ++) {  
            for (let j = i + 1; j < this.projectNameOptions.length;j ++) {
                if (this.projectNameOptions[i] == this.projectNameOptions[j]) {
                    this.projectNameOptions.splice(j, 1);
                    j--;
                }
            }
        }
        for(var i = 0; i < this.locationOptions.length;i ++) {  
            for (let j = i + 1; j < this.locationOptions.length;j ++) {
                if (this.locationOptions[i] == this.locationOptions[j]) {
                    this.locationOptions.splice(j, 1);
                    j--;
                }
            }
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加网络设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const neId = row.neId || this.ids
      getNetworkDeviceMgmt(neId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改网络设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.neId != null) {
            updateNetworkDeviceMgmt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNetworkDeviceMgmt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const neIds = row.neId || this.ids;
      this.$modal.confirm('是否确认删除网络设备管理编号为"' + neIds + '"的数据项？').then(function() {
        return delNetworkDeviceMgmt(neIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有网络设备管理数据项？').then(() => {
        this.exportLoading = true;
        return exportNetworkDeviceMgmt(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
